//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    public class WorkbookProtection : IOfficeXmlSerializable
    {
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("workbookProtection"))
            {
                _passwordHash = c.GetOptionalString("workbookPassword");
                _revisionsPassword = c.GetOptionalString("revisionsPassword");
                LockStructure = c.GetOptionalBoolean("lockStructure", false);
                LockWindows = c.GetOptionalBoolean("lockWindows", false);
                LockRevision = c.GetOptionalBoolean("lockRevision", false);
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("workbookProtection"))
            {
                c.SetOptionalString("workbookPassword", _passwordHash);
                c.SetOptionalString("revisionsPassword", _revisionsPassword);
                c.SetOptionalBoolean("lockStructure", _lockStructure, false);
                c.SetOptionalBoolean("lockWindows", lockWindows, false);
                c.SetOptionalBoolean("lockRevision", lockRevision, false);
            }
        }

        #endregion

        private string _passwordHash; //\\ change type

        [Obsolete("Use PasswordHash instead")]
        public string Password
        {
            get { return _passwordHash; }
            set { _passwordHash = value; }
        }
        public string PasswordHash
        {
            get { return _passwordHash; }
            set { _passwordHash = value; }
        }
        /// <summary>
        /// Returns true if the password corresponds to the stored password hash
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        internal bool IsValidPassword(string password)
        {
            if (string.IsNullOrEmpty(PasswordHash))
                return true;
            if (password == null)
                return false;
            return OfficeHash.GetPasswordHash(password) == this.PasswordHash;
        }
        /// <summary>
        /// Set the password hash from a given password.
        /// </summary>
        /// <param name="password">The password to hash</param>
        public void SetPasswordHash(string password)
        {
            if (string.IsNullOrEmpty(password))
                this.PasswordHash = null;
            else
            {
                this.PasswordHash = OfficeHash.GetPasswordHash(password);
            }
        }


        private string _revisionsPassword;

        public string RevisionPassword
        {
            get { return _revisionsPassword; }
            set { _revisionsPassword = value; }
        }

        private bool _lockStructure = true;

        public bool LockStructure
        {
            get { return _lockStructure; }
            set { _lockStructure = value; }
        }
        private bool lockWindows;

        public bool LockWindows
        {
            get { return lockWindows; }
            set { lockWindows = value; }
        }
        private bool lockRevision;

        public bool LockRevision
        {
            get { return lockRevision; }
            set { lockRevision = value; }
        }





    }
}
